#!/bin/bash

# run as "./scripts/update-manual.sh"

special_page="icub_manual"

echo "This script assumes you've run doxygen, and produced latex"
echo "files in ./doc/latex"
echo ""
echo "The script takes a subset of the available documentation, based"
echo "on a page called $special_page"

base=$PWD

src=$base/doc/latex
target=$base/doc/manual

rm -rf $target
cp -R $src $target
cd $target

# find the entities referred to in "manual.tex"
perl -pe "s/\\\\doxyref\\{[^\\}]*\\}\\{p\\.\\}\\{([^\\}]*)\\}/\\\\hyperlink\\{\$1\\}/g" < icub_manual.tex > canonical.tmp
perl -n -e "@b = (\$_ =~ /hyperlink\\{([^\\}]*)\\}/g ); print join(' ',@b), \"\\n\";" < canonical.tmp > order.txt
order=`cat order.txt | egrep "[a-z]"`
echo "Order of sections is: $order"
echo -n > order.tex
for f in $order; do
    echo "\\include{$f}" >> order.tex
done

# at this point, "order.tex" should have an include line for
# every entity listed in the special_page

# fix up unresolved links for material not included
echo Fixing links to unselected material
rm -f junk.tex
for f in `ls *.tex`; do
    ( 
	perl -pe "s/\\\\hyperlink\\{\\}\\{([^\\}]*)\\}/\$1/g" | \
	    perl -pe "s/p\\....pageref\\{\\}/see class docs/g" | \
	    perl -pe "s/\\\\doxyref\\{([^\\}]*)\\}\\{p\\.\\}\\{\\}/\$1/g"|\
	    perl -pe "s/(\\\\hypertarget\\{[^\\}]*\\}\\{\\})(.*)\\\\label\\{[^\\}]*\\}/\$2\$1/g" | \
	    cat
    ) < $f > junk.tex
    mv junk.tex $f
done
echo Done fixing links

# patch the main tex file to link only to what we want
# basically, every include/input gets commented out
# and one ("what_is_yarp") gets replaced with an input from "order.tex"
#sed "/YARP Page/,/END/! { /YARP File/,/YARP Page/ s/^/%%/ }" -i refman.tex
#sed "s/.*section.YARP Page.*/%%\\0/" -i refman.tex
#sed "s/.*section.Welcome to YARP.*/%%\\0/" -i refman.tex
sed "s/\\\\chapter.*/%%\\0/" -i refman.tex
sed "s/.*input.index.*/%%\\0/" -i refman.tex
sed "s/^.include.*/%%\\0/" -i refman.tex
sed "s/^.input.*/%%\\0/" -i refman.tex
sed "s/.*\\input.pages..*/\\\\input\\{order\\}/" -i refman.tex
#perl -pi -e "s/%%(.*-example.*)/\\1/" refman.tex
sed "/documentclass/,/pagenumbering/ { s/^.*Generated by Doxygen.*/\\\\input\\{${special_page}_title\\}\\\\\\\\/ }" -i refman.tex
perl -pe "s/(\\{tocdepth\\})\\{1\\}/\\{tocdepth\\}\\{2\\}/" < refman.tex > junk.tex
mv junk.tex refman.tex
for f in `ls *.tex`; do
    perl -pe "s/\\\\section/\\\\chapter/g" < $f > junk.tex
    perl -pe "s/(\\\\(sub)*)subsection/\${1}section/g" < junk.tex > $f
done

# certain lists are generated automatically, but have clumsy
# formatting.  Here we patch that up.

for f in ; do
#for f in group__dev__examples.tex; do
    sed "s/\\\\section.*Modules.*//" -i $f
    sed "s/\\\\section.Detailed.*//" -i $f
    ( perl -n -e "@b = (\$_ =~ /hyperlink\\{([^\\}]*)\\}/g ); print join(' ',@b), \"\\n\";" ) < $f > order2.txt
    order=`cat order2.txt | egrep "[a-z]"`
    for g in $order; do
	sed "s/\\\\chapter/\\\\section/" -i $g.tex
	cat $g.tex >> $f
    done
done

sed "s/.*chapter.*//" -i ${special_page}_title.tex
   
make pdf

